﻿// WMS Library - http://wmslib.codeplex.com/
// Copyright (c) Tom Gaskins 2004 - Erik Putrycz 2013
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WMSLib.Client;
using System.Net;
using System.Xml;
using System.Diagnostics;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using Nito.AsyncEx;
using WMSLib.WMSTools;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Concurrency;
using System.Threading;


namespace ULWeatherTests
{
    [TestClass]
    public class MesonetTests
    {
        [TestMethod]
        public async Task TestParseWMS2()
        {            
            var server = new Server(@"http://mesonet.agron.iastate.edu/cgi-bin/wms/goes/conus_ir.cgi?");
            var r = await server.LoadData();
            var cap = server.Capabilities;
            Assert.AreEqual(4, cap.Layers.Count);
            var fLayer = cap.Layers.First();
            var layers = fLayer.Layers;
            Assert.AreEqual(3, layers.Count);
            //,
            var lat = 45.421780;
            var lon = -75.691177;
            var l = fLayer.FindBy(cl => true, lat, lon, lat, lon);
            Assert.AreEqual(1, l.Count);
        }

        [TestMethod]
        public async Task TestReadImage2()
        {
            var svrURL = @"http://mesonet.agron.iastate.edu/cgi-bin/wms/goes/conus_ir.cgi?";
            var server = new Server(svrURL);
            var r = await server.LoadData();
            var cap = server.Capabilities;
            Assert.AreEqual(4, cap.Layers.Count);
            var fLayer = cap.Layers.First();
            var layers = fLayer.Layers;
            Assert.AreEqual(3, layers.Count);
            //,
            var lat = 45.421780;
            var lon = -75.691177;
            var l = fLayer.FindBy(cl => true, lat, lon, lat, lon);
            Assert.AreEqual(1, l.Count); 
            var ir = fLayer.FindBy(cl => true, lat, lon, lat, lon).Single();

            var w = 512;
            var h = 512;
            var dt = DateTime.Now;
            var hour = dt.Hour;
            if (dt.Minute > 30)
            {
                hour++;
            }
            var fDt = dt.Date;
            fDt = fDt.AddHours(hour);

            var bb = new WMSLib.Client.Layer.BoundingBoxType() { MinX = -74.6346271171326, MinY = 46.22741707027332, MaxX = -72.87694133662491, MaxY = 47.26928933489761 };
            var uris = ir.BuildAllURLs(w, h, bb, ImageType.ImagePng);
            Assert.AreEqual(1, uris.Count);
            var cts = new CancellationTokenSource();
            var b = await WMSUtils.ReadAllImages(cts.Token, new Subject<int>(), uris, async s => new Bitmap(s));
            Assert.AreEqual(1, b.Count);
            Assert.IsTrue(b.All(tp => tp.Item2.Width == w && tp.Item2.Height == h));

        }

    }
}
